package com.example.lanouhn.firstapp.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.model.CollectionGoods;
import com.example.lanouhn.firstapp.activity.model.Syomb;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;
import com.example.lanouhn.firstapp.activity.utils.SyombJson;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 我的收藏商品数据适配器
 * <p/>
 *
 */
public class CollectionGoodsAdapter extends RecyclerView.Adapter {

    private String key;
    private Syomb  sb;

    //上下文对象
    private Context context;
    //数据集合
    private List<CollectionGoods> CList;





    //定义监听接口
    public interface CollectionGoodsRecyclerViewListener{

        void onClick(View view,int position);
    }

    private CollectionGoodsRecyclerViewListener collectionGoodsRecyclerViewListener;

    public void setCollectionGoodsRecyclerViewListener(CollectionGoodsRecyclerViewListener collectionGoodsRecyclerViewListener) {
        this.collectionGoodsRecyclerViewListener = collectionGoodsRecyclerViewListener;
    }



    public CollectionGoodsAdapter(List<CollectionGoods> CList, Context context) {
        this.CList = CList;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载布局
        RecyclerView.ViewHolder holder = null;
        holder = new VHolder(
                LayoutInflater.from(context).
                        inflate(R.layout.item_my_collection_goods,null));

        //获取本地存储的key值
        key = (String) SPUtils.get(context,"log_key","");
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        final VHolder vHolder = (VHolder) holder;
        //数据位置
        final CollectionGoods collectionGoods = CList.get(position);
        //设置数据  运用picasso获取图片
        Picasso.with(context).load(collectionGoods.getGoods_image_url()).into(vHolder.collectionGoods_image);
        vHolder.collectionGoods_name.setText(collectionGoods.getGoods_name());
        vHolder.collectionGoods_price.setText(collectionGoods.getGoods_price());

        //recyclerView的item点击事件
        vHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collectionGoodsRecyclerViewListener.onClick(vHolder.itemView,position);

            }
        });
        //删除收藏的商品点击事件
        vHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String url = "http://www.niaolema.com/mobile/index.php?act=member_favorites&op=favorites_del";
                        String params ="fav_id=" + collectionGoods.getFav_id() + "&key=" + key;
                        String result = HttpUtils.doPost(url,params);
                        if(!result.equals("") && !result.equals("null")){
                            sb = SyombJson.getSyomb(result);

                            if(sb.getDatas() == 1){
                                //解析成功
                                handler.sendEmptyMessage(1);
                            }else {
                                handler.sendEmptyMessage(0);
                            }


                        }else {
                            handler.sendEmptyMessage(0);

                        }

                    }
                }).start();


            }
            private Handler handler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);

                    switch(msg.what){

                        case 0:
                            Toast.makeText(context, "请求失败,请稍候再试!!!", Toast.LENGTH_SHORT).show();
                            break;
                        case 1:

                            Intent intent = new Intent();
                            intent.setAction("delete");
                            context.sendBroadcast(intent);

                            break;


                    }
                }
            };

        });



    }


    @Override
    public int getItemCount() {
        return CList.size();
    }


    //定义VHolder类继承RecyclerView.ViewHolder
    class VHolder extends RecyclerView.ViewHolder{

        private ImageView collectionGoods_image;
        private TextView collectionGoods_name;
        private TextView collectionGoods_price;
        private ImageView iv_delete;

        //绑定控件id
        public VHolder(View itemView) {
            super(itemView);
            collectionGoods_image = (ImageView) itemView.findViewById(R.id.collectionGoods_image);
            collectionGoods_name = (TextView) itemView.findViewById(R.id.collectionGoods_name);
            collectionGoods_price = (TextView) itemView.findViewById(R.id.collectionGoods_price);
            iv_delete = (ImageView) itemView.findViewById(R.id.iv_delete);

        }
    }


}
